<template>
  <div class="prcList">
    <Loading v-if="loading"></Loading>
    <div class="info" v-else>
      <table class="listInfo" v-if="!showStart">
        <thead>
          <tr>
            <td>编号</td>
            <td>名称</td>
            <td>图片</td>
            <td>操作</td>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item,index) in tableData">
            <td>{{index+1}}</td>
            <td>{{item.zone_name}}</td>
            <td><img class="logo" :src="item.zone_logo"></td>
            <td>
              <a @click='addressUp(item)'>编辑</a>
              <a @click='addressDel(item.id)'>删除</a>
            </td>
          </tr>
        </tbody>
      </table>
      
      <div class="itemInfo" v-if="showStart">
          <div class="formBox">
              <p class="item"><span class="name">地区名称 ：</span><input v-model="date.zone_name" class="slong" placeholder="请输入内容"></input></p>
              <p class="item"><span class="name">地区图片 ：</span>
                  <img class="t2" :src="date.zone_logo">
                  <input type="file" @change="upImg" id="imgTest" class="mel" accept=".gif,.jpg,.jpeg,.png" name="">
              </p>
              <p class="item inputBox"><button @click="addressPut" class="submitInput">确定</button><button @click="empty" class="emptyInput">取消</button></p>
          </div>
      </div> 

    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import Loading from '@/components/loading'
import axios from 'axios'
import { Get, Post,Put,Delete } from '@/config/api'
export default {
  name: 'prcList',
  data() {
    return {
      loading: true,
      showStart:false,
      tableData: [],
      addressId:'',
      date:{
        zone_name:'',
        zone_logo:''
      }
    }
  },
  mounted() {
    this.getData()
  },
  components: {
    Loading
  },
  methods: {
    //获取数据
    async getData(val) {
      if (this.Check()) {
        let reg=await Get('/goods/zone?_sort=id',this.$store.state.token)
        this.tableData=reg.data;
        this.loading=false
      }
    },
    //编辑地区
    addressUp(e){
        this.showStart=true
        this.addressId=e.id
        this.date.zone_name=e.zone_name
        this.date.zone_logo=e.zone_logo
    },
    //删除地区
    async addressDel(e){
      if(this.Check()){
        let reg=await Delete('/goods/zone'+e,this.$store.state.token)
        alert('删除成功')
        this.showStart=false
        this.getData()
      }
    },
    //修改地区
    async addressPut(){
      if(this.date.zone_logo.length>100){
        let data={
          "zone_name":this.date.zone_name,
          "zone_logo":this.date.zone_logo
        }
        let reg=await Put('/goods/zone/'+this.addressId,this.$store.state.token,data)
        alert('修改成功')
        this.showStart=false
        this.getData()
      }else{
        let data={
          "zone_name":this.date.zone_name
        }
        console.log(data)
        let reg=await Put('/goods/zone/'+this.addressId,this.$store.state.token,data)
        alert('修改成功')
        this.showStart=false
        this.getData()
      }
        
        
    },
    //上传图片
    upImg(e){
        let file = e.target.files[0]; //input 
        if( file.size > 1*1024*1024 ){  //用size属性判断文件大小不能超过1M
          alert( "你上传的文件太大了！" ) 
        }else{
          let that=this
          let reader = new FileReader(); 
          reader.readAsDataURL(file); 
          reader.onload = function(e){ 
            var res = this.result; 
            that.date.zone_logo=this.result 
          } 
        }
    },
    //取消
    empty(){
      this.showStart=false
    }
  }
}

</script>
